#!/usr/bin/env python

# This script installs or uninstalls cmd_tutor on your system.

import os, shutil, sys

INSTALL_DIR = '/usr'

def info():
    
    print 'Run "python install.py install" as root to install cmd_tutor in /usr.'
    print 'To uninstall, simply run "python install.py uninstall".'
    print 'However you can run program right here in this directory.'
    print 'Installation is optional.'

if len(sys.argv) > 1:
    
    if os.path.isdir(INSTALL_DIR):
        
        if len(sys.argv) == 2:
            
            if sys.argv[1] == 'install':
                
                # Make new directories if they do not exist.
                if not os.path.exists(INSTALL_DIR + '/bin'):
                    os.makedirs(INSTALL_DIR + '/bin', 0777)
                if not os.path.exists(INSTALL_DIR + '/share/cmd_tutor'):
                    os.makedirs(INSTALL_DIR + '/share/cmd_tutor', 0777)
                    
                # If the directories exist, copy the files to them.
                if os.path.isdir(INSTALL_DIR + '/bin'):
                    shutil.copy('src/cmd_tutor.py', INSTALL_DIR + '/bin/cmd_tutor')
                    os.chmod(INSTALL_DIR + '/bin/cmd_tutor', 0777)
                    print 'installed ' +  INSTALL_DIR + '/bin/cmd_tutor'

                if os.path.isdir(INSTALL_DIR + '/share/cmd_tutor'):
                    shutil.copytree('share/cmd_test', INSTALL_DIR + '/share/cmd_tutor/cmd_test')
                    print 'installed ' +  INSTALL_DIR + '/share/cmd_tutor/cmd_test'
                    shutil.copytree('share/lessons', INSTALL_DIR + '/share/cmd_tutor/lessons')
                    print 'installed ' +  INSTALL_DIR + '/share/cmd_tutor/lessons'
                    #os.chmod(INSTALL_DIR + '/share/cmd_tutor/lessons', 0777)
                    #os.chmod(INSTALL_DIR + '/share/cmd_tutor/lessons', 0777)
                    #os.chmod(INSTALL_DIR + '/share/cmd_tutor/cmd_test', 0777)
                    os.system("chmod -R 777 '/usr/share/cmd_tutor'")
            elif sys.argv[1] == 'uninstall':
            
                # Remove the files if they exist
                if os.path.exists(INSTALL_DIR + '/bin/cmd_tutor'):
                    os.remove(INSTALL_DIR + '/bin/cmd_tutor')
                    print 'removed ' +  INSTALL_DIR + '/bin/cmd_tutor'
                if os.path.exists(INSTALL_DIR + '/share/cmd_tutor'):
                    shutil.rmtree(INSTALL_DIR + '/share/cmd_tutor')
                    print 'removed ' +  INSTALL_DIR + '/share/cmd_tutor'
                    
            else:
                info()
                
        else:
            info()
            
    else:
        print
        print INSTALL_DIR, 'does not exist.'
        info()
    
else:
    info()    
